package com.quanyan.ic.datacenter.dao.impl;

import com.quanyan.ic.datacenter.constant.Constants;
import com.quanyan.ic.datacenter.dao.AlarmTimePointReportOfCityDao;
import com.quanyan.ic.datacenter.http.req.ReqReport;
import com.quanyan.ic.datacenter.http.resp.RespAlarmsTimePointLineChart;
import com.quanyan.ic.datacenter.mapper.icData.AlarmTimePointReportOfCityMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmTimePointReportOfCity;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmTimePointReportOfCityExample;
import com.quanyan.ic.datacenter.model.AlarmDayReportParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/22.
 */
@Repository
public class AlarmTimePointReportOfCityDaoImpl implements AlarmTimePointReportOfCityDao {

    @Autowired
    private AlarmTimePointReportOfCityMapper alarmTimePointReportOfCityMapper;

    @Override
    public List<RespAlarmsTimePointLineChart> findAlarmTimePointReportOfCityByDay(ReqReport reqReport){
        AlarmTimePointReportOfCityExample alarmTimePointReportOfCityExample = new AlarmTimePointReportOfCityExample();
        AlarmTimePointReportOfCityExample.Criteria criteria = alarmTimePointReportOfCityExample.createCriteria();
        criteria.andDateEqualTo(reqReport.getStartDate());
        List<AlarmTimePointReportOfCity> listDb =  alarmTimePointReportOfCityMapper.selectByExample(alarmTimePointReportOfCityExample);
        List<RespAlarmsTimePointLineChart> result = new ArrayList<>();
        if ( listDb != null && listDb.size() > 0 ){
            for ( AlarmTimePointReportOfCity db : listDb ){
                RespAlarmsTimePointLineChart respAlarmsTimePointLineChart = new RespAlarmsTimePointLineChart();
                respAlarmsTimePointLineChart.setId(db.getCityId());
                respAlarmsTimePointLineChart.setName(db.getCityName());

               // respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvZero());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvThree());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvSix());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvNine());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvTwelve())
                ;respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvFifteen());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvEighteen());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvTwelve());

                //respAlarmsTimePointLineChart.getLightLineChart().add(db.getlZero());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlThree());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlSix());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlNine());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlTwelve());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlFifteen());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlEighteen());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlTwentyOne());

               // respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoZero());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoThree());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoSix());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoNine());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoTwelve());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoFifteen());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoEighteen());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoTwentyOne());
                result.add(respAlarmsTimePointLineChart);
            }
        }
        return result;
    }


    @Override
    public void insertAlarmTimePointCity(AlarmDayReportParam param){
        Integer cityId = param.getCityId()  ;
        Date date = param.getDate();
        Byte type = param.getType();
        String cityName = param.getCityName();
        Integer timeInt = param.getStartTimeInt();
        AlarmTimePointReportOfCityExample alarmTimePointReportOfCityExample = new AlarmTimePointReportOfCityExample();
        AlarmTimePointReportOfCityExample.Criteria criteria = alarmTimePointReportOfCityExample.createCriteria();
        criteria.andCityIdEqualTo(cityId);
        criteria.andDateEqualTo(date);
        List<AlarmTimePointReportOfCity> listDb =  alarmTimePointReportOfCityMapper.selectByExample(alarmTimePointReportOfCityExample);
        if ( listDb != null && listDb.size() > 0 ){
            AlarmTimePointReportOfCity city = listDb.get(0);
            AlarmTimePointReportOfCity update = new AlarmTimePointReportOfCity();
            update.setId(city.getId());

//            if ( timeInt >= 0 && timeInt < 3){
//                if ( type == Constants.AlarmsType.order ){
//                    update.setoThree(city.getoZero() + 1);
//                }else if( type == Constants.AlarmsType.alarms_type_video ){
//                    update.setvThree(city.getvZero() + 1);
//                } else if( type == Constants.AlarmsType.alarms_type_light ){
//                    update.setlThree(city.getlZero() + 1);
//                }
//
//            }else
            if ( timeInt >= 0 && timeInt < 6){
                if ( type == Constants.AlarmsType.order ){
                    update.setoThree(city.getoThree() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvThree(city.getvThree() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlThree(city.getlThree() + 1);
                }

            } else if ( timeInt >= 6 && timeInt < 9){

                if ( type == Constants.AlarmsType.order ){
                    update.setoSix(city.getoSix() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvSix(city.getvSix() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlSix(city.getlSix() + 1);
                }

            } else if ( timeInt >= 9 && timeInt < 12){
                if ( type == Constants.AlarmsType.order ){
                    update.setoNine(city.getoNine() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvNine(city.getvNine() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlNine(city.getlNine() + 1);
                }
            } else if ( timeInt >= 12 && timeInt < 15){
                if ( type == Constants.AlarmsType.order ){
                    update.setoTwelve(city.getoTwelve() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setoTwentyOne(city.getoTwelve() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlTwelve(city.getlTwelve() + 1);
                }
            }  else if ( timeInt >= 15 && timeInt < 18){
                if ( type == Constants.AlarmsType.order ){
                    update.setoFifteen(city.getoFifteen() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvFifteen(city.getvFifteen() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlFifteen(city.getlFifteen() + 1);
                }
            } else if ( timeInt >= 18 && timeInt < 24){
                if ( type == Constants.AlarmsType.order ){
                    update.setoEighteen(city.getoEighteen() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvEighteen(city.getvEighteen() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlEighteen(city.getlEighteen() + 1);
                }
            }

            alarmTimePointReportOfCityMapper.updateByPrimaryKeySelective(update);

        }else {
            AlarmTimePointReportOfCity insert = new AlarmTimePointReportOfCity();
            insert.setCityId(cityId);
            insert.setCityName(cityName);
            insert.setDate(date);

//            if ( timeInt >= 0 && timeInt < 3){
//                if ( type == Constants.AlarmsType.order ){
//                    insert.setoZero( 1);
//                }else if( type == Constants.AlarmsType.alarms_type_video ){
//                    insert.setvZero( 1);
//                } else if( type == Constants.AlarmsType.alarms_type_light ){
//                    insert.setlZero(1);
//                }
//
//            }else
            if ( timeInt >= 0 && timeInt < 6){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoThree(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvThree(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlThree(1);
                }

            } else if ( timeInt >= 6 && timeInt < 9){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoSix(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvSix(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlSix(1);
                }
            } else if ( timeInt >= 9 && timeInt < 12){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoNine(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvNine(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlNine(1);
                }
            } else if ( timeInt >= 12 && timeInt < 15){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoTwelve(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setoTwentyOne(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlTwelve(1);
                }
            }  else if ( timeInt >= 15 && timeInt < 18){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoFifteen(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvFifteen(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlFifteen(1);
                }
            } else if ( timeInt >= 18 && timeInt <= 24){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoEighteen(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvEighteen(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlEighteen(1);
                }
            }
            alarmTimePointReportOfCityMapper.insertSelective(insert);
        }
    }

}
